<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>如何设置mp3文件的ID3v1Tag</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="FastCGI_i.html">如何安装FastCGI</a>&nbsp;&nbsp;>>Next: <a href="PerlTidy.html">PerlTidy</a></p>
<h1>如何设置mp3文件的ID3v1Tag</h1>
<div class='content'>
<p>Category: <a href='Script.html'>Script</a> &nbsp; Keywords: <b>mp3 ID3v1Tag</b></p><h2>Task</h2>
从网上download了一些歌曲，回来用<a href="http://www.foobar2000.org">foobar2000</a>放。发现没有显示其歌曲名和歌手名。<br>
因为<a href="http://www.minilyrics.com/">迷你歌词/MiniLyrics</a>查找配对歌词要通过mp3的歌曲名和歌手名。一个个修改太麻烦了。就想着用perl写个批量修改。

<h2>Code</h2>
<a href="http://search.cpan.org">cpan search</a>了下，install MP3::ID3v1Tag<br>
此模块perldoc说的很清楚，我也就照本宣科写了个代码。<br>
其中获得artist,title,ablum的过程不通用。各位自己去parse
<pre>
#!/usr/bin/perl
# usage: set one directory mp3's ID3v1Tag, such as artist, title, ablum
use MP3::ID3v1Tag;

my $dir = "E:/Music/Santana_Shaman";
opendir(DIR,"$dir");
my @data = readdir(DIR);
closedir(DIR);
@data = grep(/\.mp3$/, @data);

foreach $mp3 (@data) {
	my $mp3_file = new MP3::ID3v1Tag("$dir/$mp3");
	#if($mp3_file->got_tag()) {
		# it's special,here the file name like "Santana Shaman 11 Feels Like Fire Feat DIDO.mp3"
		# u parse file name and got the artist&album&title&..
		my ($artist, $album, undef, $title) = ($mp3 =~ /^([a-zA-Z]+)\s+([a-zA-Z]+)\s+([0-9]+)\s+(.*?)\.mp3$/);
		print "$artist, $album, $title\n";
		#end
		$mp3_file->set_title("$title");
		$mp3_file->set_artist("$artist");
		$mp3_file->set_album("$album");
		#$mp3_file->set_year(2002);
		#$mp3_file->set_genre("Latin-Rock");
		$mp3_file->save() or warn "failed to save $mp3";
  #}
}
</pre></div>
<p><&lt;Previous: <a href="FastCGI_i.html">如何安装FastCGI</a>&nbsp;&nbsp;>>Next: <a href="PerlTidy.html">PerlTidy</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AEmp3%E6%96%87%E4%BB%B6%E7%9A%84ID3v1Tag&url=http://www.fayland.org/journal/mp3_ID3v1Tag.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='wwwsf4.cn.html'>WWW Security FAQ: CGI Scripts</a> < <span class='digit'>2004-11-10 22:30:44</span> ></li><li><a href='GoogleGroup.html'>WWW::Mechanize && Google Group</a> < <span class='digit'>2005-04-13 18:49:43</span> ></li></ul>
Created on <span class="digit">2004-11-17 22:08:34</span>, Last modified on <span class="digit">2004-11-17 22:09:49</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>